﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BySoft.Server.Common.Enum;
using BySoft.Server.Business;
using BySoft.Web.Utility;
using BySoft.Server.Common.Entity;
using BySoft.Server.Consts;
using BySoft.Server.Common;
using System.Data;
using OneSoft.EntityCore;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
using System.Text;

namespace BySoft.Web.Site.ComPage
{
    public partial class SetCheckData : BasePopPage
    {
        #region 变量定义
        protected DataTable DtCheckList
        {
            get
            {
                if (Session["DtCheckParts"] != null)
                {
                    return (DataTable)Session["DtCheckParts"];
                }
                else
                {
                    return CreateDT();
                }
            }
            set
            {
                Session["DtCheckParts"] = value;
            }
        }
        private DataTable CreateDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("MatInstockDetailID");//0
            dt.Columns.Add("PurchaseDetailID");//1 
            dt.Columns.Add("PurchaseID");//2 
            dt.Columns.Add("WorkOrderNo");//4
            dt.Columns.Add("MaterialID");//5
            dt.Columns.Add("MaterialName");//6
            dt.Columns.Add("Specifications");//7
            dt.Columns.Add("Unit");//8
            dt.Columns.Add("PurchaseNum");//9
            dt.Columns.Add("LeftNums");//10
            dt.Columns.Add("RevNum");//11
            dt.Columns.Add("Price");//12
            dt.Columns.Add("Amount");//13
            dt.Columns.Add("StockID");//13
            dt.Columns.Add("StockName");//13
            dt.Columns.Add("CheckStatus");//13
            dt.Columns.Add("StatusName");//13 
            return dt;
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindParameterForListControl("UnqualifiedDoType", ddlUnqualifiedDoType);
                UIHelper.InsertFirstItem(ddlUnqualifiedDoType);
                hidDetailID.Value = UIHelper.GetQueryStringValue("id");
                InitData();
            }
        }

        void InitData()
        {
            DataTable dtTemp = DtCheckList.Copy();
            DataRow[] drs = dtTemp.Select("MatInstockDetailID=" + CommonMethod.ConvertToDecimal(hidDetailID.Value) + "");
            if (drs != null && drs.Length > 0)
            {
                lblReceiveNum.Text = CommonMethod.GetString(drs[0]["RevNum"]);
            }
            lblQualifiedNum.Text = lblReceiveNum.Text;
            lblPrice.Text = CommonMethod.GetString(drs[0]["Price"]);
            plGiveInNum.Visible = false;
            plUnQualifiedNum.Visible = false;

        }


        protected void ddlResultType_Changed(object sender, EventArgs e)
        {
            try
            {
                plGiveInNum.Visible = false;
                plUnQualifiedNum.Visible = false;
                lblQualifiedNum.Text = lblReceiveNum.Text;
                txtCompromiseNum.Text = "";
                txtUnQualifiedNum.Text = "";
                txtCompromiseAmount.Text = "";
                ddlUnqualifiedDoType.SelectedValue = "";
                txtUnqualifiedReason.Text = "";
                if (ddlResultType.SelectedValue == "3")
                {
                    plGiveInNum.Visible = true;
                    plUnQualifiedNum.Visible = false;
                }
                else if (ddlResultType.SelectedValue == "5")
                {
                    plGiveInNum.Visible = false;
                    plUnQualifiedNum.Visible = true;
                }
            }
            catch (Exception ex)
            {
                UIHelper.AlertMessage(this.Page, ex.ToString());
            }
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                if (!ValidateData())
                {
                    return;
                }
                //List<BsMatinstockdetailEntity> lty = new List<BsMatinstockdetailEntity>();
                //List<StStockmanagedetailEntity> ltyStockDetail = new List<StStockmanagedetailEntity>();
                //List<StStockmanagematEntity> ltyStockmanger = new List<StStockmanagematEntity>();
                //DataTable dtTemp = DtCheckList.Copy();
                //DataRow[] drs = dtTemp.Select("MatInstockDetailID=" + CommonMethod.ConvertToDecimal(hidDetailID.Value) + "");
                //if (drs != null && drs.Length > 0)
                //{
                //    foreach (DataRow gvr in drs)
                //    {
                //        DataRow dr = drs[0];
                //        BsMatinstockdetailEntity entity = new BsMatinstockdetailEntity();
                //        entity.MatInstockDetailID = CommonMethod.ConvertToDecimal(hidDetailID.Value.Trim());
                //        string strCheckStatus = CommonMethod.GetString(ddlResultType.SelectedValue.Trim()); 
                //        entity.CheckStatus = strCheckStatus; 
                //        if (strCheckStatus == "1")
                //        {
                //            entity.QualifiedNum = CommonMethod.ConvertToDecimal(lblReceiveNum.Text.Trim());
                //            entity.UnqualifiedNum = 0;
                //            entity.CompromiseNum = 0;
                //            entity.CompromiseAmount = 0;
                //            entity.UnqualifiedReason ="";
                //            entity.UnqualifiedDoType = "";
                //        }
                //        else if (strCheckStatus == "5")
                //        {
                //            entity.QualifiedNum = CommonMethod.ConvertToDecimal(lblQualifiedNum.Text.Trim());
                //            entity.UnqualifiedNum = CommonMethod.ConvertToDecimal(txtUnQualifiedNum.Text.Trim());
                //            entity.CompromiseNum = 0;
                //            entity.CompromiseAmount = 0;
                //            entity.UnqualifiedReason = CommonMethod.GetString(txtUnqualifiedReason.Text.Trim());
                //            entity.UnqualifiedDoType = "";
                //        }
                //        else if (strCheckStatus == "3")
                //        {
                //            entity.QualifiedNum = CommonMethod.ConvertToDecimal(lblQualifiedNum.Text.Trim());
                //            entity.UnqualifiedNum = 0;
                //            entity.CompromiseNum = CommonMethod.ConvertToDecimal(txtCompromiseNum.Text.Trim());
                //            entity.CompromiseAmount = CommonMethod.ConvertToDecimal(txtCompromiseAmount.Text.Trim());
                //            entity.UnqualifiedReason ="";
                //            entity.UnqualifiedDoType = CommonMethod.GetString(ddlUnqualifiedDoType.SelectedValue.Trim()); 
                //        }
                //        lty.Add(entity);
                //        if (strCheckStatus == "1" || (strCheckStatus == "5" && CommonMethod.ConvertToDecimal(lblQualifiedNum.Text.Trim()) > 0))
                //        {
                //            StStockmanagedetailEntity enStockDetail = new StStockmanagedetailEntity();
                //            enStockDetail.OrderDetailID = CommonMethod.ConvertToInt(dr["OrderDetailID"]);
                //            enStockDetail.OrderType = CommonMethod.GetString(dr["OrderType"]);
                //            enStockDetail.SupplierID = CommonMethod.GetString(dr["SupplierID"]);
                //            enStockDetail.FormName = "材料入库单";
                //            enStockDetail.FormID = CommonMethod.GetString(dr["MatInstockID"]);
                //            string strFormType = ((int)BaseEnum.FormType.MaterialInStock).ToString();
                //            enStockDetail.FormType = strFormType;
                //            string strReceiveDate = CommonMethod.GetString(dr["BillDate"]);
                //            if (!string.IsNullOrEmpty(strReceiveDate))
                //            {
                //                enStockDetail.FormDate = Convert.ToDateTime(strReceiveDate);
                //            }
                //            else { enStockDetail.FormDate = DateTime.Now; }
                //            enStockDetail.FormOperator = CurrentUser.UserID.Trim();
                //            enStockDetail.FormDetailID = CommonMethod.GetString(dr["MatInstockDetailID"]);
                //            enStockDetail.StockID = CommonMethod.GetString(dr["StockID"]);
                //            enStockDetail.PartsID = CommonMethod.GetString(dr["MaterialID"]);
                //            enStockDetail.PartsName = CommonMethod.GetString(dr["MaterialName"]);
                //            enStockDetail.TypeID = "";
                //            enStockDetail.PartsType = "1";
                //            enStockDetail.ChangeType = "1";
                //            enStockDetail.PartsNum = CommonMethod.ConvertToDecimal(lblQualifiedNum.Text.Trim());
                //            enStockDetail.Price = CommonMethod.ConvertToDecimal(lblPrice.Text.Trim());
                //            ltyStockDetail.Add(enStockDetail);
                //        } 
                //    }
                //}
                //StStockmanagedetail.ListCheck(lty, ltyStockDetail);
                //string sReturn = "success";
                //ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setCookie(dialogReturn_key,'" + sReturn + "',1);CloseTopDialog();</script>");
            }
            catch (Exception ex)
            {
                UIHelper.AlertMessage(this.Page, ex.Message.ToString());
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            try
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>CloseTopDialog();</script>");
            }
            catch (Exception ex)
            {
                UIHelper.Alert(this.Page, ex.ToString().Trim());
            }

        }

        private bool ValidateData()
        {
            decimal dRecieve = CommonMethod.ConvertToDecimal(lblReceiveNum.Text);
            decimal dGiveInNum = CommonMethod.ConvertToDecimal(txtCompromiseNum.Text);
            decimal dUnQualifiedNum = CommonMethod.ConvertToDecimal(txtUnQualifiedNum.Text);
            string strMsg = "";
            if (string.IsNullOrEmpty(ddlResultType.SelectedValue))
            {
                UIHelper.AlertMessage(this.Page, "请选择检验结果！");
                return false;
            }
            if (ddlResultType.SelectedValue == "3")
            {
                lblQualifiedNum.Text = (dRecieve - dGiveInNum).ToString();
                if (dGiveInNum<=0)
                {
                    UIHelper.AlertMessage(this.Page, "请填写让步数量！");
                    return false;
                }
            }
            if (ddlResultType.SelectedValue == "5")
            {
                lblQualifiedNum.Text = (dRecieve - dUnQualifiedNum).ToString();
                if (dUnQualifiedNum<=0)
                {
                    UIHelper.AlertMessage(this.Page, "请填写不合格数量！");
                    return false;
                }
            }

            return true;
        }
    }
}